package com.panshi.controller.qa;

import com.panshi.api.ICommentApi;
import com.panshi.utils.GetSessionUser;
import com.panshi.vo.qa.Comment;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class CommentController {
    @Autowired
    ICommentApi commentApi;

    /**
     * 评论添加
     * @param comment
     * @return
     */
    @PostMapping("/comment/add")
    @ResponseBody
    DataResultQA addComment(@RequestBody Comment comment){
        //获取登录后的用户id
        int loginUserId = GetSessionUser.getSessionUserId();
        comment.setCUserId(loginUserId);
        DataResultQA dataResultQA = commentApi.addComment(comment);
        return dataResultQA;
    }


    /**
     * 分页查询
     * @param pages
     * @return
     */
    @PostMapping("/comment/inquirePages")
    @ResponseBody
    DataResultQA<Pages<Comment>> inquirePagesComment(@RequestBody Pages pages){
        DataResultQA<Pages<Comment>> dataResultQA = commentApi.inquirePagesComment(pages);
        return dataResultQA;
    }

    /**
     * 分页查询
     * @param pages
     * @return
     */
    /*
    @PostMapping("/comment/inquirePagesDataHtml")
    String inquirePagesCommentHtml(Map<String, Object> model, @RequestBody Pages pages){
        DataResultQA<Pages<Comment>> dataResultQA = qaApi.inquirePagesComment(pages);
        List<Comment> listComment = dataResultQA.getData().getList();
        model.put("listComment",listComment);
        return "/answers/list_comment_data";
    }*/

    /**
     * 分页查询回复
     * @param pages
     * @return
     */
    @PostMapping("/comment/inquirePagesRevert")
    @ResponseBody
    DataResultQA<Pages<Comment>> inquirePagesRevert(@RequestBody Pages pages){
        DataResultQA<Pages<Comment>> dataResultQA = commentApi.inquirePagesRevert(pages);
        return dataResultQA;
    }

    /**
     * 查询总记录数
     * @param pages
     * @return
     */
    @PostMapping("/comment/inquireSum")
    @ResponseBody
    DataResultQA<Integer> inquireCommentSum(@RequestBody Pages pages){
        DataResultQA<Integer> dataResultQA = commentApi.inquireCommentSum(pages);
        return dataResultQA;
    }

    /**
     * 通过id查询评论
     * @param id
     * @return
     */
    @PostMapping("/comment/inquireCommentById/{id}")
    @ResponseBody
    DataResultQA<Comment> inquireCommentById(@PathVariable int id){
        DataResultQA<Comment> dataResultQA = commentApi.inquireCommentById(id);
        return dataResultQA;
    }

    /**
     * 通过id查询回复
     * @param id
     * @return
     */
    @PostMapping("/comment/inquireRevertById/{id}")
    @ResponseBody
    DataResultQA<Comment> inquireRevertById(@PathVariable int id){
        DataResultQA<Comment> dataResultQA = commentApi.inquireRevertById(id);
        return dataResultQA;
    }

    /**
     * 删除(标记删除)
     * @param id
     * @return
     */
    @PostMapping("/comment/delete/{id}")
    @ResponseBody
    DataResultQA deleteComment(@PathVariable int id){
        DataResultQA dataResultQA = commentApi.deleteComment(id);
        return dataResultQA;
    }

    /**
     * 评论点赞
     * @param id
     * @return
     */
    @PostMapping("/comment/giveALike/{id}")
    @ResponseBody
    DataResultQA giveALike(@PathVariable int id){
        DataResultQA dataResultQA = commentApi.giveALike(id);
        return dataResultQA;
    }

    /**
     * 评论赞减一
     * @param id
     * @return
     */
    @PostMapping("/comment/cancelGiveALike/{id}")
    @ResponseBody
    DataResultQA cancelGiveALike(@PathVariable int id){
        DataResultQA dataResultQA = commentApi.cancelGiveALike(id);
        return dataResultQA;
    }

    /**
     * 用于用户查询自己发布的评论
     * @param pages
     * @return
     */
    @PostMapping("/comment/inquireMy")
    @ResponseBody
    DataResultQA<Pages<Comment>> inquireMyComment(@RequestBody Pages pages){
        DataResultQA<Pages<Comment>> dataResultQA = commentApi.inquireMyComment(pages);
        return dataResultQA;
    }
}
